/**
 * @author Jason
 * Request parameters by this activity:
 * ID        the tracking number
 * SOURCE    page source which indicates the parent activity
 */
package com.cats.activity;

import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cats.R;
import com.cats.adapter.ShipmentAdapter;
import com.cats.model.ShipmentDetailModel;
import com.cats.model.ShipmentUpdateModel;

public class ShipmentActivity extends BaseActivity {
	private ListView listView = null;
	private ShipmentAdapter adapter = null;
	private ShipmentDetailModel detailModel = null;
	private String sourcePageString = null;
	
//	private ProgressBar pb;

	public ShipmentActivity() {
		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shipment);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		setActionBarTitle("Shipment");
				
		Bundle b = getIntent().getExtras();
		String idString = b.getString("ID");
		sourcePageString = b.getString("SOURCE");
		int status = b.getInt("STATUS");		
		System.out.println("[ShipmentActivity]:onCreate() with ID" + idString);
		setContentView(R.layout.activity_shipment);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		TextView textView = (TextView)findViewById(R.id.textView2);
		textView.setText(idString);
		
//		pb = (ProgressBar)findViewById(R.id.progressBar1);
//		pb.setVisibility(View.GONE);
		
		
		// TODO: start request
		// assume we've got data
		detailModel = new ShipmentDetailModel();
		if (status == 0) {
			detailModel.setStatus(0);
			ArrayList<ShipmentUpdateModel> updates = new ArrayList<ShipmentUpdateModel>();
			ShipmentUpdateModel object1 = new ShipmentUpdateModel("Dec 11, 2013 12:05", "Acceptance in Little Rock, AR");
			updates.add(object1);
			detailModel.setUpdates(updates);
		}
		else if (status == 1) {
			detailModel.setStatus(1);
			ArrayList<ShipmentUpdateModel> updates = new ArrayList<ShipmentUpdateModel>();
			ShipmentUpdateModel object1 = new ShipmentUpdateModel("Dec 11, 2013 12:05", "Acceptance in Little Rock, AR");
			ShipmentUpdateModel object2 = new ShipmentUpdateModel("Dec 12, 2013 01:21", "Depart for Dallas, TX");
			ShipmentUpdateModel object3 = new ShipmentUpdateModel("Dec 13, 2013 09:23", "Arrived at Dallas, TX");
			ShipmentUpdateModel object4 = new ShipmentUpdateModel("Dec 14, 2013 08:21", "Depart for Houston, TX");
			updates.add(object1);
			updates.add(object2);
			updates.add(object3);
			updates.add(object4);
			detailModel.setUpdates(updates);			
		}
		else if (status == 2) {
			detailModel.setStatus(2);
			ArrayList<ShipmentUpdateModel> updates = new ArrayList<ShipmentUpdateModel>();
			ShipmentUpdateModel object1 = new ShipmentUpdateModel("Dec 11, 2013 12:05", "Acceptance in Little Rock, AR");
			ShipmentUpdateModel object2 = new ShipmentUpdateModel("Dec 12, 2013 01:21", "Depart for Dallas, TX");
			ShipmentUpdateModel object3 = new ShipmentUpdateModel("Dec 13, 2013 09:23", "Arrived at Dallas, TX");
			ShipmentUpdateModel object4 = new ShipmentUpdateModel("Dec 14, 2013 01:21", "Depart for Houston, TX");
			ShipmentUpdateModel object5 = new ShipmentUpdateModel("Dec 14, 2013 09:23", "Arrived at Houston, TX");
			ShipmentUpdateModel object6 = new ShipmentUpdateModel("Dec 15, 2013 15:23", "Delivered, Houston, TX");
			updates.add(object1);
			updates.add(object2);
			updates.add(object3);
			updates.add(object4);
			updates.add(object5);
			updates.add(object6);
			detailModel.setUpdates(updates);			
		}


		// TODO: put in request callback
		// set status bar
		switch (detailModel.getStatus()) {
		case 0: // Initiated
		{
			TextView statusTextView = (TextView)findViewById(R.id.statusText);
			statusTextView.setText(getResources().getString(R.string.initiated));
			ImageView statusImageView = (ImageView)findViewById(R.id.statusImage);
			statusImageView.setImageResource(R.drawable.status_initiated);
		}
			break;
		case 1: // In trasit
		{
			TextView statusTextView = (TextView)findViewById(R.id.statusText);
			statusTextView.setText(getResources().getString(R.string.in_transit));
			ImageView statusImageView = (ImageView)findViewById(R.id.statusImage);
			statusImageView.setImageResource(R.drawable.status_trasit);
		}			
			break;		
		case 2: // Delivered
		{
			TextView statusTextView = (TextView)findViewById(R.id.statusText);
			statusTextView.setText(getResources().getString(R.string.delivered));
			ImageView statusImageView = (ImageView)findViewById(R.id.statusImage);
			statusImageView.setImageResource(R.drawable.status_delivered);
		}
			break;
		default:
			break;
		}
		
		
		// init listview
		if (detailModel.getUpdates().size() > 0) {
			adapter = new ShipmentAdapter(this, R.layout.shipment_listview_item, detailModel.getUpdates());
			listView = (ListView)findViewById(R.id.updateListView);
			listView.setAdapter(adapter);			
		}

		//adapter.notifyDataSetChanged();
	}
	
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent parentActivityIntent;
			try {
				parentActivityIntent = new Intent(this, Class.forName(sourcePageString));
				parentActivityIntent.addFlags(
						Intent.FLAG_ACTIVITY_CLEAR_TOP |
		                Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(parentActivityIntent);
		        finish();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

            return true;
		}
		
		
		return super.onOptionsItemSelected(item);
	}
	
	
	
	/*
	private class MyAsyncTask extends AsyncTask<String, Integer, Double> {
		private final static String URL_STRING = "http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl";
		 
		@Override
		protected Double doInBackground(String... params) {
			// TODO Auto-generated method stub
			postData(params[0]);
			return null;
		}
 
		protected void onPostExecute(Double result){
			pb.setVisibility(View.GONE);
		}
		
		protected void onProgressUpdate(Integer... progress){
			pb.setProgress(progress[0]);
		}
 
		public void postData(String valueIWantToSend) {
			// Create a new HttpClient and Post Header
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(URL_STRING);
 
			try {
				// Add your data
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
				nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 
				// Execute HTTP Post Request
				HttpResponse response = httpclient.execute(httppost);
				System.out.println("response:" + response.getAllHeaders().toString());
 
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
		}
 
	}
	*/
}




